﻿using SyncCore.Sync.Models;
using SyncDemo.Sync.Models;
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SyncCore.Sync.Helpers
{
    public class MessagePackHelper
    {
        ///<summary>进行创建房间时，用于房主的，IP地址转消息包。</summary>
        public static MessagePack FromServerAddressPortInfoToMessagePack(AddressPortInfo info)
        {
            var messagePack = new MessagePack();
            messagePack.Type = MessageType.FromServer;
            messagePack.Commands = new CommandMessagePack[]
            {
                 new CommandMessagePack(info.ToString(), info.ToString()),
            };

            return messagePack;
        }

        ///<summary>在房主建立房间后，客户能接受到的来自房主的，消息包转IP地址转换。</summary>
        public static AddressPortInfo FromServerMessagePackToAddressPortInfo(MessagePack messagePack)
        {
            if (messagePack.Type != MessageType.FromServer) throw new Exception("不支持该格式的信息");

            var commandPack = messagePack.Commands.FirstOrDefault();
            if (commandPack == null) return null;
            var command = commandPack.Command;

            AddressPortInfo.TryParse(command, out var addressPortInfo);
            return addressPortInfo;
        }

        ///<summary>创建成功了。</summary>
        public static MessagePack CreateSuccess()
        {
            return new MessagePack() { Type = MessageType.Success };
        }

        public static MessagePack CreateFailed()
        {
            return new MessagePack() { Type = MessageType.Failed };
        }

        public static MessagePack CreateWaiting()
        {
            return new MessagePack() { Type = MessageType.Waiting };
        }
    }
}
